package com.example.blog.modules.article.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.article.dto.CommentDTO;
import com.example.blog.modules.article.dto.CommentRequest;
import com.example.blog.modules.article.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "评论管理", description = "评论管理相关接口")
@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    @Operation(summary = "创建评论")
    @PostMapping
    @HasPermission("comment:create")
    public ApiResponse<CommentDTO> createComment(@Valid @RequestBody CommentRequest request) {
        return ApiResponse.success(commentService.createComment(request));
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/{id}")
    @HasPermission("comment:delete")
    public ApiResponse<Void> deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return ApiResponse.success();
    }

    @Operation(summary = "获取评论详情")
    @GetMapping("/{id}")
    @HasPermission("comment:read")
    public ApiResponse<CommentDTO> getComment(@PathVariable Long id) {
        return ApiResponse.success(commentService.getComment(id));
    }

    @Operation(summary = "获取文章评论列表")
    @GetMapping("/article/{articleId}")
    @HasPermission("comment:read")
    public ApiResponse<Page<CommentDTO>> getArticleComments(
            @PathVariable Long articleId,
            Pageable pageable) {
        return ApiResponse.success(commentService.getArticleComments(articleId, pageable));
    }

    @Operation(summary = "获取评论回复列表")
    @GetMapping("/{commentId}/replies")
    @HasPermission("comment:read")
    public ApiResponse<List<CommentDTO>> getCommentReplies(@PathVariable Long commentId) {
        return ApiResponse.success(commentService.getCommentReplies(commentId));
    }

    @Operation(summary = "点赞评论")
    @PostMapping("/{id}/like")
    @HasPermission("comment:like")
    public ApiResponse<Void> likeComment(@PathVariable Long id) {
        commentService.incrementLikeCount(id);
        return ApiResponse.success();
    }

    @Operation(summary = "取消点赞评论")
    @DeleteMapping("/{id}/like")
    @HasPermission("comment:like")
    public ApiResponse<Void> unlikeComment(@PathVariable Long id) {
        commentService.decrementLikeCount(id);
        return ApiResponse.success();
    }

    @Operation(summary = "审核评论")
    @PostMapping("/{id}/approve")
    @HasPermission("comment:approve")
    public ApiResponse<Void> approveComment(@PathVariable Long id) {
        commentService.approveComment(id);
        return ApiResponse.success();
    }

    @Operation(summary = "获取文章已审核评论数量")
    @GetMapping("/article/{articleId}/approved-count")
    @HasPermission("comment:read")
    public ApiResponse<Long> getApprovedCommentCount(@PathVariable Long articleId) {
        return ApiResponse.success(commentService.getApprovedCommentCount(articleId));
    }
} 